{% extends 'main_hub/user_base.html' %}

{% block title %}
    我的视频
{% endblock %}

{% block css %}
    <style>
        #loading {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            z-index: 15000;
        }

        #loading div {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 80px;
            height: 80px;
            margin-top: -15px;
            margin-left: -15px;
        }
    </style>
{% endblock %}


{% if video_error %}
    <script>
        alert('{{ video_error }}');
        window.location.href = "{% url 'user'%}"
    </script>
{% endif %}

{% block content %}
    <br>
    <!-- 所发布的视频 -->
    <div class="container">
        <div class="row">
            <div class="col-auto">
                <!-- 发布视频按钮 -->
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#uplodeVideo"
                        data-whatever="@mdo">
                    点击发布视频
                </button>
            </div>
            <!-- 发布视频表单 -->
            <div class="modal fade" id="uplodeVideo" tabindex="-1" aria-labelledby="exampleModalLabel"
                 aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">发布视频</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <form method="POST" action="{% url 'user_video' %}" enctype="multipart/form-data"
                                  onsubmit="on_submit()">
                                {% csrf_token %}
                                <div class="form-group">
                                    <label for="recipient-name" class="col-form-label">视频标题:</label>
                                    <input type="text" class="form-control" id="video_name" name="name" required
                                           lay-verify="required">
                                </div>
                                <div class="form-group">
                                    <label for="message-text" class="col-form-label">视频封面:</label>
                                    <div class="input-group mb-3">
                                        <div class="custom-file">
                                            <input type="file" class="custom-file-input" id="video_image" name="image"
                                                   required
                                                   lay-verify="required"
                                                   onchange="CheckFile(this,['jpg', 'png','webp'])" accept="image/*">
                                            <label class="custom-file-label" for="video_image"
                                                   aria-describedby="video_imageAddon02">选择文件</label>
                                        </div>
                                        <div class="input-group-append sr-only">
                                            <span class="input-group-text" id="video_imageAddon02"></span>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="message-text" class="col-form-label">视频文件:</label>
                                    <div class="input-group mb-3">
                                        <div class="custom-file">
                                            <input type="file" class="custom-file-input" id="video_field"
                                                   name="user_field"
                                                   required lay-verify="required"
                                                   onchange="CheckFile(this,['mp4', 'avi', 'mov'])" accept="video/*">
                                            <label class="custom-file-label" for="video_field"
                                                   aria-describedby="video-imageAddon">选择文件</label>
                                        </div>
                                        <div class="input-group-append sr-only">
                                            <span class="input-group-text" id="video-imageAddon"></span>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="recipient-name" class="col-form-label">视频简介:</label>
                                    <input type="text" class="form-control" id="video_info" name="info" required
                                           lay-verify="required">
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                                    <button type="submit" class="btn btn-primary">提交</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <hr>

        <div id="loading">
            <div class="spinner-border text-light" role="status">
            </div>
            <div class="text-danger" style="top: 60%;left:46.5%;width: auto;font-size: 20px;text-align: center">
                <span style="font-weight: bolder">正在上传请勿关闭此页面</span></div>
        </div>

        <!-- 视频 -->
        <div class="row">
            {% for video in all_video.object_list %}
                <div style="margin: 10px">
                    <div class="card" style="width:12rem;">
                        <a href="{% url 'video_detail' video.id %}?video_sub_number=1">
                            <img class="card-img-top" src="{{ MEDIA_URL }}{{ video.image }}" style="width: 12rem">
                        </a>
                        <div class="card-body">
                            <h5 class="card-title info">
                                <a href="{% url 'video_detail' video.id %}?video_sub_number=1"
                                   style="text-decoration:none;color:black;font-size: 15px;font-weight: bold">
                                    {% if video.name|length > 8 %}
                                        {{ video.name|slice:'0:8' }}...
                                    {% else %}
                                        {{ video.name }}
                                    {% endif %}</a>
                            </h5>
                            <p class="card-text">
                                {% if video.info|length > 9 %}
                                    {{ video.info|slice:'0:8' }}...
                                {% else %}
                                    {{ video.info }}
                                {% endif %}</p>
                            <a data-toggle="modal"
                               data-target="#delete_video{{ forloop.counter }}" style="cursor:pointer"><img
                                    src="/static/img/trash.svg">
                            </a>
                            <div class="modal fade" id="delete_video{{ forloop.counter }}" tabindex="-1"
                                 aria-labelledby="delete_video{{ forloop.counter }}Label" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="delete_video{{ forloop.counter }}Label">
                                                视频删除</h5>
                                            <button type="button" class="close" data-dismiss="modal"
                                                    aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <div class="modal-body" align="center">
                                            <div class="card" style="width: 18rem;">
                                                <img src="{{ MEDIA_URL }}{{ video.image }}" class="card-img-top"
                                                     alt="{{ video.name }}">
                                                <div class="card-body">
                                                    <p class="card-text">{{ video.name }}</p>
                                                </div>
                                            </div>
                                            <p class="card-text" style="color: red">您确定删除此视频吗？删除后不能恢复</p>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">
                                                关闭
                                            </button>
                                            <button type="button" class="btn btn-danger"
                                                    onclick="user_video_delete({{ video.id }})">确定删除
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>

        <!-- 分页 -->
        <div class="row justify-content-center">
            <div class="col-auto" style="margin-top: 50px">
                <nav aria-label="Page navigation example">
                    <ul class="pagination" style="right:500px">
                        {% if all_video.has_previous %}
                            <li class="page-item">
                                <a href="?{{ all_video.previous_page_number.querystring }}"
                                   class="page-link" aria-label="Previous">
                                    上一页
                                </a>
                            </li>
                        {% endif %}
                        {% for page in all_video.pages %}
                            {% if page %}
                                {% ifequal page all_video.number %}
                                    <li class="page-item">
                                        <a class="page-link" href="?{{ page.querystring }}">{{ page }}</a>
                                    </li>
                                {% else %}
                                    <li>
                                        <a class="page-link page" href="?{{ page.querystring }}">{{ page }}</a>
                                    </li>
                                {% endifequal %}
                            {% else %}
                                <li class="none">...</li>
                            {% endif %}
                        {% endfor %}
                        {% if all_video.has_next %}
                            <li class="page-item">
                                <a href="?{{ all_video.next_page_number.querystring }}" class="page-link"
                                   aria-label="Next">下一页</a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            </div>
        </div>
    </div>



{% endblock %}

{% block custom_jss %}
    <script>
        {#隐藏遮罩#}
        $('#loading').hide();


        {# 文件类型判断 #}
        function CheckFile(obj, TypeArray) {
            var fileName = $(obj).val();
            $(obj).next('.custom-file-label').html(fileName);
            var array = TypeArray; //可以上传的文件类型
            if (obj.value == '') {
                alert("选择要上传的文件!");
                return false;
            } else {
                var fileContentType = obj.value.match(/^(.*)(\.)(.{1,8})$/)[3];
                var isExists = false;
                for (var i in array) {
                    if (fileContentType.toLowerCase() == array[i].toLowerCase()) {
                        isExists = true;
                        return true;
                    }
                }
                if (isExists == false) {
                    $(obj).next('.custom-file-label').html('选择文件');
                    alert("上传文件类型不正确!");
                    return false;
                }
                return false;
            }
        }

        function on_submit() {
            $('#loading').show();
        }


        {# 视频删除 #}
        function user_video_delete(video_id) {
            $.ajax({
                cache: false,
                type: "POST",
                url: '{% url 'user_video_delete' %}',
                data: {'video_id': video_id},
                async: true,
                beforeSend: function (xhr, setting) {
                    xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                },
                success: function (data) {
                    if (data.status == 'fail') {
                        alert(data.msg);
                        window.location.reload()
                    } else if (data.status == 'success') {
                        alert(data.msg);
                        window.location.reload()
                    }
                }
            })
        }

    </script>
{% endblock %}